package com.tgit.sso.paltform.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import javax.sql.DataSource;
import java.sql.SQLException;
/**
 * 
* @ClassName: DruidConfig   
* @Description: TODO(数据源配置)   
* @author 范再军  
* @date 2018年9月14日 下午12:42:46   
*
 */
@Configuration
@DependsOn("ConfigInfo")
@PropertySource(value = "classpath:config.properties")
public class DruidConfig implements EnvironmentAware {


	private RelaxedPropertyResolver propertyResolver;

	@Override
	public void setEnvironment(Environment env) {
		this.propertyResolver = new RelaxedPropertyResolver(env,"spring.datasource.");
	}

    @Bean(destroyMethod = "close", initMethod = "init")
	public DataSource mysqlDataSource() throws SQLException {
        DruidDataSource dataSource = new DruidDataSource();

        dataSource.setUrl(ConfigInfo.PROPERTY_MAP.getProperty("url"));
        dataSource.setUsername(ConfigInfo.PROPERTY_MAP.getProperty("username"));
        //对数据库进行解密处理
        try {
            dataSource.setPassword(ConfigInfo.PROPERTY_MAP.getProperty("password"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        dataSource.setDriverClassName(ConfigInfo.PROPERTY_MAP.getProperty("driver.class"));

        //propertyResolver赋值，从application.properties中获取
        dataSource.setInitialSize(Integer.parseInt(propertyResolver.getProperty("initialSize")));  
        dataSource.setMaxActive(Integer.parseInt(propertyResolver.getProperty("maxActive")));  
        dataSource.setMinIdle(Integer.parseInt(propertyResolver.getProperty("minIdle")));  
        dataSource.setMaxWait(Integer.parseInt(propertyResolver.getProperty("maxWait")));  
        dataSource.setTimeBetweenEvictionRunsMillis(Integer.parseInt(propertyResolver.getProperty("timeBetweenEvictionRunsMillis")));  
        dataSource.setMinEvictableIdleTimeMillis(Integer.parseInt(propertyResolver.getProperty("minEvictableIdleTimeMillis")));  
        dataSource.setValidationQuery(propertyResolver.getProperty("validationQuery"));  
        dataSource.setTestOnBorrow(Boolean.getBoolean(propertyResolver.getProperty("testOnBorrow")));  
        dataSource.setTestWhileIdle(Boolean.getBoolean(propertyResolver.getProperty("testWhileIdle")));  
        dataSource.setTestOnReturn(Boolean.getBoolean(propertyResolver.getProperty("testOnReturn")));  
        dataSource.setPoolPreparedStatements(Boolean.getBoolean(propertyResolver.getProperty("poolPreparedStatements")));  
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(Integer.parseInt(propertyResolver.getProperty("maxPoolPreparedStatementPerConnectionSize")));
        dataSource.setFilters(propertyResolver.getProperty("filters"));  
        return dataSource;
	}

}
